import os
import gtk
import ConfigParser
from BuildWizException import BuildWizException

class FileConfigurator:
    #def __init__(self, *args): this approach could be used to constructor overloading
    def __init__( self, param):
        self.__config = ConfigParser.ConfigParser()
        if isinstance( param, dict):
            self.__create( param)
        elif isinstance( param, str):
            self.__load( param)

    def __load( self, filename):
        if os.path.isfile( filename):
            self.__fileName = filename
            try:
                self.__config.read( self.__fileName)
                #for section in self.__config.sections():
                #    print section
                #    for option in self.__config.options( section):
                #        print " ", option, "=", self.__config.get( section, option)

            except ConfigParser.MissingSectionHeaderError:
                raise BuildWizException( "file [%s] is missing section header" % self.__fileName)

    def __create( self, values):
        self.__fileName = values[ "file"]

        self.addSection( "platform")
        self.set( "platform", "name", values[ "name"])
        self.set( "platform", "description", values[ "descr"])
        self.set( "platform", "projectdir", values[ "projdir"])
        self.set( "platform", "cmakelistsdir", values[ "cmakelists"])
        self.set( "platform", "builddir", values[ "builddir"])

        self.addSection( "subversion")
        self.set( "subversion", "verify_updates", "False")
        self.set( "subversion", "url", "")
        self.set( "subversion", "current_revision", "")
        self.set( "subversion", "user", "")
        self.set( "subversion", "password", "")

        self.addSection( "make")
        self.set( "make", "jobs", "0")
        self.set( "make", "run_install", "False")

        self.addSection( "CMAKE_VARIABLES_NAMES")
        self.set( "CMAKE_VARIABLES_NAMES", "names", "")

        self.addSection( "CMAKE_DEFINITIONS_NAMES")
        self.set( "CMAKE_DEFINITIONS_NAMES", "names", "")

        self.addSection( "scripts")
        self.set( "scripts", "after", "")
        self.set( "scripts", "run_before", "False")
        self.set( "scripts", "run_after", "False")
        self.set( "scripts", "before", "") 

        self.addSection( "deploy_pen")
        self.set( "deploy_pen", "dir_name", "")
        self.set( "deploy_pen", "destiny_dir", "")
        self.set( "deploy_pen", "link_2_dest_dir", "")
        self.set( "deploy_pen", "umount_after_deploy", "False")
        self.set( "deploy_pen", "dir_name_datetime", "")
        self.set( "deploy_pen", "source_dir", "")
        self.set( "deploy_pen", "deploy_after_build_process", "False")

        self.addSection( "preferences")
        self.set( "preferences", "font_output_window", "False")

        self.write()

    def getKeyValue( self, key, value):
        try:
            value = self.__config.get( key, value)
            return value
        except ConfigParser.NoSectionError, error:
            raise BuildWizException( "no section \"%s\" found in [%s]" % (key, self.__fileName))
        except ConfigParser.NoOptionError, error:
            raise BuildWizException( "no option \"%s\" found in [%s]" % (value, self.__fileName))

    def addSection( self, name):
        try:
            self.__config.add_section( name)
            return True
        except ConfigParser.DuplicateSectionError:
            return False

    def set( self, section, key, value):
        self.__config.set( section, key, value)

    def write( self):
        try:
            fp = file( self.__fileName, "w")
            self.__config.write( fp)
            fp.close()
        except IOError, error:
            dlg = gtk.MessageDialog( None, 0, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, error)
            dlg.run()
            dlg.destroy()

    def deleteSections( self, sections):
        for name in sections:
            self.__config.remove_section( name)

